package com.river.stack;

import com.google.gson.Gson;


public class ArrayStack<T> {
    int size;
    private int count;

    private Object[] items;

    public ArrayStack(int size) {
        this.size = size;
        items = new Object[size];
    }

    public boolean put(T item){
        if(count >= size){
            return false;
        }
        items[count++] = item;
        return true;
    }
    public T pop(){
        if(count == 0){
            return null;
        }
        T item = (T)items[count-1];
        items[count-1] = null;
        count--;
        return item;
    }
    public void show(){
        System.out.println(new Gson().toJson(this));
    }

    public static void main(String[] args) {
        ArrayStack<String> arrayStack = new ArrayStack<>(5);
        arrayStack.put("hello");
        arrayStack.put("river");
        arrayStack.put("fan");
        arrayStack.put("frank");
        arrayStack.put("luck");
        arrayStack.put("lily");
        arrayStack.show();
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        arrayStack.show();
    }


}
